﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System.Diagnostics;

namespace BaGiaDanhCuop
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        // Constructor
        UIElementRendererHelper uiRenderer;
        
        //random
        Random random = new Random();

        //entity
        public static OldWoman Woman;
        Stick[] GameStick = new Stick[5];

        //state
        public const int STATE_MOVE = 0;
        public const int STATE_FAIL = 1;
        public const int STATE_STRONG = 2;
        public const int STATE_MID = 3;
        public const int STATE_BECUFFED = 4;
        public const int STATE_WEAPON = 5; //Guy

        public const int ITEM_EMPTY = 0;
        public const int ITEM_TENNIS = 1;
        public const int ITEM_BASEBALL = 2;
        public const int ITEM_GOLF = 3;
        public const int ITEM_LIGHT = 4;

        public const int DAY = 0;
        public const int AFTERNOON = 1;
        public const int NIGHT = 2;

        public static int money = 10000000;
        public static int coin = 10000000;
        public float GameTime = 0;

        ////woman
        Texture2D Woman_Shadow_TT;

        //stick 
        Texture2D[] Move_TT             = new Texture2D[5];
        Texture2D[] Fighting_Fail_TT    = new Texture2D[5];
        Texture2D[] Fighting_Strong_TT  = new Texture2D[5];
        Texture2D[] Fighting_Mid_TT     = new Texture2D[5];

        //power
        Texture2D PowerBackground_TT, Power_TT;

        //Guy
        Guy[] GameGuy = new Guy[6];//creat
        List<Guy> ListGuy = new List<Guy>();//run 

        Texture2D Guy01_Move_TT, Guy01_Mid_TT, Guy01_Strong_TT, Guy01_Weapon_TT;
        Texture2D Guy02_Move_TT, Guy02_Mid_TT, Guy02_Strong_TT, Guy02_Weapon_TT;
        Texture2D Guy03_Move_TT, Guy03_Mid_TT, Guy03_Strong_TT, Guy03_Weapon_TT;
        Texture2D Guy04_Move_TT, Guy04_Mid_TT, Guy04_Strong_TT, Guy04_Weapon_TT;
        Texture2D Guy05_Move_TT, Guy05_Mid_TT, Guy05_Strong_TT, Guy05_Weapon_TT;
        Texture2D Guy06_Move_TT, Guy06_Mid_TT, Guy06_Strong_TT, Guy06_Weapon_TT;

        public static Rectangle PowerRectangle;
        public static bool Power_Touch = false;
        public static bool Power_Status = false;
        public static bool PauseGame = false;

        public GamePage()
        {
            InitializeComponent();

            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromMilliseconds(5);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;

            //random state background
            ShopPage.Backgrounds.state = random.Next(0, 3);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            uiRenderer = new UIElementRendererHelper(this);

            LoadContent();
            // TODO: use this.content to load your game content here

            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            uiRenderer.Dispose();

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>

        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
            double delta = e.ElapsedTime.TotalSeconds;


            checkTouch();
            //Debug.WriteLine("delta: " + delta*200);
            if (PauseGame == false)
            {
                //background
                ShopPage.Backgrounds.Update((float)delta);

                //power
                if (Power_Touch && PowerRectangle.Width < Power_TT.Width)
                {
                    PowerRectangle.Width += (int)(delta * 200);
                }
                if (Power_Status)
                {
                    Change_Power();
                    Woman.UpdateState();
                    for (int i = 0; i < 5; i++)
                    {
                        GameStick[i].UpdateState();
                    }
                }

                switch (Woman.state)
                {
                    case STATE_MOVE:
                        Woman.Woman_Move_Animation.Update(delta);
                        Woman.Woman_Move_Animation.Offset.Y = 460 - Woman.Woman_Move_TT.Height / 2;

                        for (int i = 0; i < 5; i++)
                        {
                            if (Woman.item == i)
                            {
                                GameStick[i].Move_Animation.Update(delta);
                                GameStick[i].Move_Animation.Offset.Y = 460 - GameStick[i].Move_TT.Height / 2;
                                GameStick[i].state = STATE_MOVE;
                            }
                        }
                        break;

                    case STATE_STRONG:
                        Woman.Woman_Fighting_Strong_Animation.Update(delta);

                        for (int i = 0; i < 5; i++)
                        {
                            if (Woman.item == i)
                            {
                                GameStick[i].Fighting_Strong_Animation.Update(delta);
                                GameStick[i].Fighting_Strong_Animation.Offset.Y = 460 - GameStick[i].Fighting_Strong_TT.Height;
                                GameStick[i].state = STATE_STRONG;
                            }
                        }

                        Woman.Woman_Fighting_Strong_Animation.Offset.Y = 460 - Woman.Woman_Fighting_Strong_TT.Height;
                        break;

                    case STATE_MID:
                        Woman.Woman_Fighting_Mid_Animation.Update(delta);

                        for (int i = 0; i < 5; i++)
                        {
                            if (Woman.item == i)
                            {
                                GameStick[i].Fighting_Mid_Animation.Update(delta);
                                GameStick[i].Fighting_Mid_Animation.Offset.Y = 460 - GameStick[i].Fighting_Mid_TT.Height;
                                GameStick[i].state = STATE_MID;
                            }
                        }

                        Woman.Woman_Fighting_Mid_Animation.Offset.Y = 460 - Woman.Woman_Fighting_Mid_TT.Height;
                        break;

                    case STATE_FAIL:
                        Woman.Woman_Fighting_Fail_Animation.Update(delta);

                        for (int i = 0; i < 5; i++)
                        {
                            if (Woman.item == i)
                            {
                                GameStick[i].Fighting_Fail_Animation.Update(delta);
                                GameStick[i].Fighting_Fail_Animation.Offset.Y = 460 - GameStick[i].Fighting_Fail_TT.Height / 2;
                                GameStick[i].state = STATE_FAIL;
                            }
                        }

                        Woman.Woman_Fighting_Fail_Animation.Offset.Y = 460 - Woman.Woman_Fighting_Fail_TT.Height / 2;
                        break;

                    case STATE_BECUFFED:
                        Woman.Woman_Be_Cuffed_Animation.Update(delta);

                        Woman.Woman_Be_Cuffed_Animation.Offset.Y = 460 - Woman.Woman_Be_Cuffed_TT.Height / 2;
                        break;

                }

                // guy
                Creat_Guy((float)delta);
                for (int i = 0; i < ListGuy.Count; i++)
                {
                    ListGuy[i].Update((float)delta);
                    if (ListGuy[i].position.X + ListGuy[i].strong_ani.FrameSize.X < 0)
                    {
                        ListGuy.Remove(ListGuy[i]);
                    }
                }
            }
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your drawing code here
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            uiRenderer.PreRender();
            uiRenderer.Draw();

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            //background
            ShopPage.Backgrounds.Draw(spriteBatch);

            //SkillPower
            spriteBatch.Draw(PowerBackground_TT, new Vector2(600, 20), Color.White);
            spriteBatch.Draw(Power_TT, new Vector2(600, 20), PowerRectangle, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0);

            //Guy
            for (int i = 0; i < ListGuy.Count; i++)
            {
                ListGuy[i].Draw(spriteBatch);
            }

            if (Woman.state == STATE_MOVE)
            {
                spriteBatch.Draw(Woman.Shadow_TT, new Vector2(Woman.position.X, 445), Color.White);
                Woman.Woman_Move_Animation.Draw(spriteBatch, SpriteEffects.None);


                for (int i = 0; i < 5; i++)
                {
                    if (Woman.item == i)
                    {
                        GameStick[i].Move_Animation.Draw(spriteBatch, SpriteEffects.None);
                    }
                }
            }
            else if (Woman.state == STATE_MID)
            {
                spriteBatch.Draw(Woman.Shadow_TT, new Vector2(Woman.position.X, 445), Color.White);
                Woman.Woman_Fighting_Mid_Animation.Draw(spriteBatch, SpriteEffects.None);

                for (int i = 0; i < 5; i++)
                {
                    if (Woman.item == i)
                    {
                        GameStick[i].Fighting_Mid_Animation.Draw(spriteBatch, SpriteEffects.None);
                    }
                }
            }
            else if (Woman.state == STATE_STRONG)
            {
                spriteBatch.Draw(Woman.Shadow_TT, new Vector2(Woman.position.X, 445), Color.White);
                Woman.Woman_Fighting_Strong_Animation.Draw(spriteBatch, SpriteEffects.None);

                for (int i = 0; i < 5; i++)
                {
                    if (Woman.item == i)
                    {
                        GameStick[i].Fighting_Strong_Animation.Draw(spriteBatch, SpriteEffects.None);
                    }
                }
            }
            else if (Woman.state == STATE_FAIL)
            {
                spriteBatch.Draw(Woman.Shadow_TT, new Vector2(Woman.position.X, 445), Color.White);
                Woman.Woman_Fighting_Fail_Animation.Draw(spriteBatch, SpriteEffects.None);

                for (int i = 0; i < 5; i++)
                {
                    if (Woman.item == i)
                    {
                        GameStick[i].Fighting_Fail_Animation.Draw(spriteBatch, SpriteEffects.None);
                    }
                }
            }
            else if (Woman.state == STATE_BECUFFED)
            {
                spriteBatch.Draw(Woman.Shadow_TT, new Vector2(Woman.position.X, 445), Color.White);
                Woman.Woman_Be_Cuffed_Animation.Draw(spriteBatch, SpriteEffects.None);
            }
            spriteBatch.End();
        }

        public void Change_Power()
        {
            if (PowerRectangle.Width > 0 && PowerRectangle.Width < Power_TT.Width * 3/ 4)
            {
                Woman.state = STATE_MID;
                for (int i = 0; i < 5; i++)
                {
                    if (Woman.item == i)
                    {
                        GameStick[i].state = STATE_MID;

                        for (int j = 0; j < ListGuy.Count; j++)
                        {
                            if (Woman.Woman_Fighting_Mid_Animation.FrameIndex >= 2)
                            {
                                Woman.state = ListGuy[j].ColletionWoman(GameStick[i]);
                                GameStick[i].state = Woman.state;
                            }
                        }
                    }
                    //Debug.WriteLine("state " + i + ": " + GameStick[i].state);
                }
            }
            else if (PowerRectangle.Width > Power_TT.Width * 3 / 4)
            {
                Woman.state = STATE_STRONG;
                for (int i = 0; i < 5; i++)
                {
                    if (Woman.item == i)
                    {
                        GameStick[i].state = STATE_STRONG;

                        for (int j = 0; j < ListGuy.Count; j++)
                        {
                            if (Woman.Woman_Fighting_Strong_Animation.FrameIndex >= 2)
                            {
                                Woman.state = ListGuy[j].ColletionWoman(GameStick[i]);
                                GameStick[i].state = Woman.state;
                            }
                        }
                    }
                }
            }
        }

        float timeCreatGuy = 0;
        //Random random = new Random();

        public void Creat_Guy(float deltatime)
        {
            timeCreatGuy += deltatime;
            if (ListGuy.Count == 0)
            {
                //Debug.WriteLine("here creat!!!!");
                timeCreatGuy = 0;
                int r = random.Next(0, 5);
                ListGuy.Add(GameGuy[r].GetGuy());
            }
        }

        public void LoadContent()
        {
            Power_TT = contentManager.Load<Texture2D>("Asset/Skill_Power");
            PowerBackground_TT = contentManager.Load<Texture2D>("Asset/Skill_Power_Background");
            PowerRectangle = new Rectangle(0, 0, 0, Power_TT.Height);

            ////woman
            Woman_Shadow_TT = contentManager.Load<Texture2D>("Asset/Shadow");
           
            Fighting_Fail_TT[0]     = contentManager.Load<Texture2D>("Asset/Batoong_Fighting_Fail_TT");
            Fighting_Mid_TT[0]      = contentManager.Load<Texture2D>("Asset/Batoong_Fighting_Mid_TT");
            Fighting_Strong_TT[0]   = contentManager.Load<Texture2D>("Asset/Batoong_Fighting_Strong_TT");
            Move_TT[0]              = contentManager.Load<Texture2D>("Asset/Batoong_Move_TT");

            Fighting_Fail_TT[1]     = contentManager.Load<Texture2D>("Asset/Tennis_Fighting_Fail_TT");
            Fighting_Mid_TT[1]      = contentManager.Load<Texture2D>("Asset/Tennis_Fighting_Mid_TT");
            Fighting_Strong_TT[1]   = contentManager.Load<Texture2D>("Asset/Tennis_Fighting_Strong_TT");
            Move_TT[1]              = contentManager.Load<Texture2D>("Asset/Tennis_Move_TT");

            Fighting_Fail_TT[2]     = contentManager.Load<Texture2D>("Asset/Baseball_Fighting_Fail_TT");
            Fighting_Mid_TT[2]      = contentManager.Load<Texture2D>("Asset/Baseball_Fighting_Mid_TT");
            Fighting_Strong_TT[2]   = contentManager.Load<Texture2D>("Asset/Baseball_Fighting_Strong_TT");
            Move_TT[2]              = contentManager.Load<Texture2D>("Asset/Baseball_Move_TT");

            Fighting_Fail_TT[3]     = contentManager.Load<Texture2D>("Asset/Golf_Fighting_Fail_TT");
            Fighting_Mid_TT[3]      = contentManager.Load<Texture2D>("Asset/Golf_Fighting_Mid_TT");
            Fighting_Strong_TT[3]   = contentManager.Load<Texture2D>("Asset/Golf_Fighting_Strong_TT");
            Move_TT[3]              = contentManager.Load<Texture2D>("Asset/Golf_Move_TT");

            Fighting_Fail_TT[4]     = contentManager.Load<Texture2D>("Asset/Light_Fighting_Fail_TT");
            Fighting_Mid_TT[4]      = contentManager.Load<Texture2D>("Asset/Light_Fighting_Mid_TT");
            Fighting_Strong_TT[4]   = contentManager.Load<Texture2D>("Asset/Light_Fighting_Strong_TT");
            Move_TT[4]              = contentManager.Load<Texture2D>("Asset/Light_Move_TT");

            for (int i = 0; i < 5; i++)
            {
                GameStick[i] = new Stick(Move_TT[i], Fighting_Fail_TT[i], Fighting_Strong_TT[i], Fighting_Mid_TT[i], 100, 200, STATE_MOVE);
            }

            //Guy
            Guy01_Move_TT   = contentManager.Load<Texture2D>("Asset/Guy01_Move");
            Guy01_Mid_TT    = contentManager.Load<Texture2D>("Asset/Guy01_Mid");
            Guy01_Strong_TT = contentManager.Load<Texture2D>("Asset/Guy01_Strong");
            Guy01_Weapon_TT = contentManager.Load<Texture2D>("Asset/Guy01_Vu_Khi");

            Guy02_Move_TT   = contentManager.Load<Texture2D>("Asset/Guy02_Move");
            Guy02_Mid_TT    = contentManager.Load<Texture2D>("Asset/Guy02_Mid");
            Guy02_Strong_TT = contentManager.Load<Texture2D>("Asset/Guy02_Strong");
            Guy02_Weapon_TT = contentManager.Load<Texture2D>("Asset/Guy02_Vu_Khi");

            Guy03_Move_TT   = contentManager.Load<Texture2D>("Asset/Guy03_Move");
            Guy03_Mid_TT    = contentManager.Load<Texture2D>("Asset/Guy03_Mid");
            Guy03_Strong_TT = contentManager.Load<Texture2D>("Asset/Guy03_Strong");
            Guy03_Weapon_TT = contentManager.Load<Texture2D>("Asset/Guy03_Vu_Khi");

            Guy04_Move_TT   = contentManager.Load<Texture2D>("Asset/Guy04_Move");
            Guy04_Mid_TT    = contentManager.Load<Texture2D>("Asset/Guy04_Mid");
            Guy04_Strong_TT = contentManager.Load<Texture2D>("Asset/Guy04_Strong");
            Guy04_Weapon_TT = contentManager.Load<Texture2D>("Asset/Guy04_Vu_Khi");

            Guy05_Move_TT   = contentManager.Load<Texture2D>("Asset/Guy05_Move");
            Guy05_Mid_TT    = contentManager.Load<Texture2D>("Asset/Guy05_Mid");
            Guy05_Strong_TT = contentManager.Load<Texture2D>("Asset/Guy05_Strong");
            Guy05_Weapon_TT = contentManager.Load<Texture2D>("Asset/Guy05_Vu_Khi");

            Guy06_Move_TT   = contentManager.Load<Texture2D>("Asset/Guy06_Move");
            Guy06_Mid_TT    = contentManager.Load<Texture2D>("Asset/Guy06_Mid");
            Guy06_Strong_TT = contentManager.Load<Texture2D>("Asset/Guy06_Strong");
            Guy06_Weapon_TT = contentManager.Load<Texture2D>("Asset/Guy06_Vu_Khi");

            GameGuy[0] = new Guy(Guy01_Move_TT, Guy01_Mid_TT, Guy01_Strong_TT, Guy01_Weapon_TT, Woman_Shadow_TT, STATE_MOVE);
            GameGuy[1] = new Guy(Guy02_Move_TT, Guy02_Mid_TT, Guy02_Strong_TT, Guy02_Weapon_TT, Woman_Shadow_TT, STATE_MOVE);
            GameGuy[2] = new Guy(Guy03_Move_TT, Guy03_Mid_TT, Guy03_Strong_TT, Guy03_Weapon_TT, Woman_Shadow_TT, STATE_MOVE);
            GameGuy[3] = new Guy(Guy04_Move_TT, Guy04_Mid_TT, Guy04_Strong_TT, Guy04_Weapon_TT, Woman_Shadow_TT, STATE_MOVE);
            GameGuy[4] = new Guy(Guy05_Move_TT, Guy05_Mid_TT, Guy05_Strong_TT, Guy05_Weapon_TT, Woman_Shadow_TT, STATE_MOVE);
            GameGuy[5] = new Guy(Guy06_Move_TT, Guy06_Mid_TT, Guy06_Strong_TT, Guy06_Weapon_TT, Woman_Shadow_TT, STATE_MOVE);
        }

        //checktouch
        public void checkTouch()
        {
            TouchCollection touch = TouchPanel.GetState();
            foreach (TouchLocation tch in touch)
            {
                CheckButton(tch);
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            PauseGame = true;
        }

        // xử lý sự kiện khi touch true
        public void CheckButton(TouchLocation touch)
        {
            if (touch.State == TouchLocationState.Pressed && contain(touch.Position.X, touch.Position.Y))
            {
                Power_Touch = true;
                //if (Woman.state < 4)
                //    Woman.state += 1;
                //else Woman.state = 0;
                //if (Woman.item < 4)
                //    Woman.item += 1;
                //else Woman.item = 0;
                //Debug.WriteLine("state: " + Woman.item);
            }
            else if(touch.State == TouchLocationState.Released){
                Power_Touch = false;
                Power_Status = true;
            }
            
        }

        //true : touch vào level
        public Boolean checkTouchButton(TouchLocation touch)
        {
            if (touch.State == TouchLocationState.Pressed)
            {
                return contain(touch.Position.X, touch.Position.Y);
            }
            return false;
        }

        //true : nằm trong vùng chọn /false : ko nằm trong
        public Boolean contain(double x, double y)
        {
            return x >= 0 && x <= 800 && y >= 0 && y <= 480;
        }
    }
}